/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.iss.examsys.ejb;

import com.iss.examsys.entity.ExamSession;
import com.iss.examsys.entity.Module;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;

/**
 *
 * @author User
 */
@Stateless
public class InvigilateExamBean {
    @PersistenceContext EntityManager em;
    
    private Module selectedExamModule;

    public Module getSelectedExamModule() {
        return selectedExamModule;
    }

    public void setSelectedExamModule(Module selectedExamModule) {
        this.selectedExamModule = selectedExamModule;
    }
   
    public List<ExamSession> findAllExams(){
        TypedQuery<ExamSession> result = em.createNamedQuery("ExamSession.findAll", ExamSession.class);
        return result.getResultList();
    }
    /**
     * Retrieve the list of exams as based on the curret date
     * @return 
     */
    public List<ExamSession> getListOfExam(){
        DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
        Date date = new Date();
        String currentDate;
        List<ExamSession> examSessions = new ArrayList<ExamSession>();
        List<ExamSession> currentExamSessions = new ArrayList<ExamSession>();
        currentDate = dateFormat.format(date);
        
        examSessions = findAllExams();
        
        if(examSessions.size() > 0){
            for(Iterator iter = examSessions.iterator();iter.hasNext();){
                ExamSession examSession = (ExamSession) iter.next();
                String examDate = dateFormat.format(examSession.getDateTime());
                
                if(examDate.equalsIgnoreCase(currentDate)){
                    currentExamSessions.add(examSession);
                }
            }
        }
  
        return currentExamSessions;
    }   
}
